package com.zfzs.post.modules.agent.service.impl;

import com.zfzs.post.modules.agent.domain.AgentWithdrawPwd;
import com.zfzs.post.modules.agent.repository.AgentWithdrawPwdRepository;
import com.zfzs.post.modules.agent.service.AgentWithdrawPwdService;
import com.zfzs.post.modules.agent.service.dto.AgentWithdrawPwdDTO;
import com.zfzs.post.modules.agent.service.dto.AgentWithdrawPwdQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentWithdrawPwdMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Optional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
* @author ck
* @date 2019-09-05
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentWithdrawPwdServiceImpl implements AgentWithdrawPwdService {

    @Autowired
    private AgentWithdrawPwdRepository agentWithdrawPwdRepository;

    @Autowired
    private AgentWithdrawPwdMapper agentWithdrawPwdMapper;

    @Override
    public Object queryAll(AgentWithdrawPwdQueryCriteria criteria, Pageable pageable){
        Page<AgentWithdrawPwd> page = agentWithdrawPwdRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentWithdrawPwdMapper::toDto));
    }

    @Override
    public Object queryAll(AgentWithdrawPwdQueryCriteria criteria){
        return agentWithdrawPwdMapper.toDto(agentWithdrawPwdRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public AgentWithdrawPwd findById(Long id) {
        Optional<AgentWithdrawPwd> agentWithdrawPwd = agentWithdrawPwdRepository.findById(id);
        ValidationUtil.isNull(agentWithdrawPwd,"AgentWithdrawPwd","id",id);
        return agentWithdrawPwd.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentWithdrawPwd create(AgentWithdrawPwd resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return agentWithdrawPwdRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentWithdrawPwd resources) {
        Optional<AgentWithdrawPwd> optionalAgentWithdrawPwd = agentWithdrawPwdRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalAgentWithdrawPwd,"AgentWithdrawPwd","id",resources.getId());
        AgentWithdrawPwd agentWithdrawPwd = optionalAgentWithdrawPwd.get();
        agentWithdrawPwd.copy(resources);
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        agentWithdrawPwdRepository.save(agentWithdrawPwd);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentWithdrawPwdRepository.deleteById(id);
    }
}